package com.zhangyu.shadow.repair.delegate.history;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.zhangyu.shadow.repair.base.IStringDataConvert;
import com.zhangyu.shadow.repair.delegate.bean.CommonBean;
import com.zhangyu.shadow.repair.delegate.bean.VehicleBean;
import com.zhangyu.shadow.repair.manager.AccountManager;

import org.greenrobot.greendao.annotation.NotNull;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import st.lib.base.net.callback.IError;

/**
 * @author zhangyu
 * @date 2018/12/4
 */

public class HistoryPresenter extends HistoryContract.Presenter {
    private final HistoryContract.View mView;
    private final HistoryContract.Model mModel;
    /**
     * 是否正在请求
     */
    private boolean isRefreshing = false;

    public HistoryPresenter(@NotNull HistoryContract.View view) {
        mView = view;
        mModel = new HistoryModel();
    }

    @Override
    void getHistoryList(final List<String> busVmTypeName, final List<String> classificationId,
                        final String depId, final String code, final String dateStart,
                        final String dateEnd, final int busVmType, final String minTime) {
        if (isRefreshing) {
            return;
        }
        isRefreshing = true;
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                SearchBean searchBean = new SearchBean();
                searchBean.setOrgId(AccountManager.getInstance().getOrgId());
                searchBean.setAppId(AccountManager.getInstance().getAppId());
                searchBean.setAuthKey(AccountManager.getInstance().getAuthKey());
                searchBean.setBusVmType(busVmType);
                searchBean.setBusVmTypeName(busVmTypeName);
                searchBean.setClassificationId(classificationId);
                searchBean.setDeptId(depId);
                searchBean.setMinTime(minTime);
                searchBean.setSearchStart(dateStart);
                searchBean.setSearchEnd(dateEnd);
                searchBean.setSourceType(AccountManager.getInstance().getSourceType());
                searchBean.setVehicleCode(code);
                searchBean.setWorkCode(AccountManager.getInstance().getWorkCode());
                emitter.onNext(JSONObject.toJSONString(searchBean));
            }
        }).observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(String content) {
                        Log.e("onNext", content);
                        addDisposable(mModel.getHistoryList(content,
                                new IStringDataConvert<ArrayList<VehicleBean>>() {
                                    @Override
                                    public void convert(ArrayList<VehicleBean> list) {
                                        if (list != null) {
                                            if (mView != null) {
                                                mView.hideLoading();
                                                mView.showHistoryList(list);
                                            }
                                        } else {
                                            if (mView != null) {
                                                mView.showHistoryList(null);
                                            }
                                        }
                                        isRefreshing = false;
                                    }
                                }, new IError() {
                                    @Override
                                    public void onError(int code, String msg) {
                                        if (mView != null) {
                                            mView.showHistoryList(null);
                                        }
                                        isRefreshing = false;
                                    }
                                })
                        );
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        isRefreshing = false;
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    @Override
    void getMaintainType() {
        addDisposable(mModel.getMaintainType(new IStringDataConvert<ArrayList<MaintainType>>() {
            @Override
            public void convert(ArrayList<MaintainType> o) {
                if (mView != null) {
                    mView.showMaintainList(o);
                }
            }
        }, new IError() {
            @Override
            public void onError(int code, String msg) {

            }
        }));
    }

    @Override
    void getRepairType() {
        addDisposable(mModel.getRepairType(new IStringDataConvert<ArrayList<MaintainType>>() {
            @Override
            public void convert(ArrayList<MaintainType> o) {
                if (mView != null) {
                    mView.showMaintainList(o);
                }
            }
        }, new IError() {
            @Override
            public void onError(int code, String msg) {

            }
        }));
    }

    @Override
    void getClassification() {
        addDisposable(mModel.getClassification(new IStringDataConvert<ArrayList<CommonBean>>() {
            @Override
            public void convert(ArrayList<CommonBean> o) {
                if (mView != null) {
                    mView.showClassificationList(o);
                }
            }
        }, new IError() {
            @Override
            public void onError(int code, String msg) {

            }
        }));
    }
}
